<template>
  <div class="content">
    <div class="container-box-left container-box">
      <div class="title">
        <div class="title-info">
          <span>用户平台</span>
        </div>
      </div>
      <el-input
          v-model="searchFormNumber"
          class="value-item"
          placeholder="请输入"
          clearable
      >
        <template #suffix>
          <el-icon
              class="el-input__icon"
              style="cursor: pointer"
              @click="getList(1)"
          ><Search
          /></el-icon>
        </template>
      </el-input>
      <div class="list-content">
        <el-tree
            ref="deptTreeDataRef"
            default-expand-all
            :data="deptTreeData"
            :props="defaultProps"
            highlight-current
            @node-click="handleNodeClick"
            :filter-node-method="filterNode"
            check-strictly
            :check-on-click-node="false"
            :expand-on-click-node="false"
            :check-on-click-leaf="false"
        >
          <template #default="{ node }">
            <span class="custom-tree-node" :title="node.label">
              <span>{{ node.label }}</span>
            </span>
          </template></el-tree
        >
      </div>
    </div>
    <div class="container-box-right">
      <div class="container-box">
        <div class="form-box form-search-box">
          <search-box>
            <template #form>
              <el-form :inline="true" class="form-wrap">
                <el-form-item label="菜单名称：">
                  <el-input
                      v-model="formInline.name"
                      placeholder="请输入"
                      clearable
                  />
                </el-form-item>
                <el-form-item label="菜单编号：">
                  <el-input
                      v-model="formInline.code"
                      placeholder="请输入"
                      clearable
                  />
                </el-form-item>
              </el-form>
            </template>
            <template #btn>
              <el-button
                  type="primary"
                  :loading="searchLoading"
                  @click="getList(1)"
              >查询</el-button
              >
              <el-button type="primary" plain @click="onSubmit">重置</el-button>
            </template>
          </search-box>
          <div class="form-add2">
          </div>
        </div>
        <div class="table-box">
          <el-table
              :data="tableData"
              style="width: 100%"
              row-key="id"
              border
              lazy
              :load="load"
              :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          >
            <el-table-column prop="name" label="菜单名称" min-width="200" header-align="center"/>
            <el-table-column prop="code" label="菜单编号" width="200" header-align="center"/>
            <el-table-column prop="sort" label="菜单排序" width="100" align="center"/>
            <el-table-column prop="status" label="菜单状态" width="100" align="center"/>
              <el-table-column prop="alias" label="菜单别名" width="150" align="center"/>
            <el-table-column prop="remark" label="菜单说明" min-width="100" align="center" show-overflow-tooltip/>
            <el-table-column label="操作" width="230" align="center">
              <template v-slot="{ row }">
                <el-button link type="primary" @click="editHandle(row)"
                >编辑</el-button
                >
                <el-button link type="primary" @click="addChildHandle(row)"
                >新增子项
                </el-button>
                <el-button v-if="row.status == 1" link type="primary" @click="statueChange(row,true)"
                >启用</el-button
                >
                <el-button v-else link type="danger" @click="statueChange(row,false)"
                >停用</el-button
                >
                <el-button link type="danger" @click="deleteMenuHandle(row)"
                >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>

        <div v-if="false" class="pagination-box">
          <el-pagination
              v-model:current-page="formInline.pageIndex"
              v-model:page-size="formInline.pageSize"
              :page-sizes="[20, 30, 40, 50]"
              layout="total, prev, pager, next,sizes, jumper"
              :total="total"
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {list, listLazy} from "@/views/menuManagement/list";
import {useRouter} from "vue-router";
const router = useRouter()
const searchFormNumber = ref("");
const total = ref(400);
const carTypeList = ref([]);
const tableData = ref([]);
const searchLoading = ref(false);
const deptTreeDataRef = ref();
const deptTreeData = ref([]);
const formInline = ref({
  workNumber: "",
  name: "",
  carType: "",
  departmentId: "",
  // date: "",
  pageIndex: 1,
  pageSize: 20,
});
const defaultProps = {
  children: "children",
  label: "name",
};
const handleNodeClick = (data) => {
  formInline.value.departmentId = data.id;
  getList();
};
const filterNode = (value, data) => {
  if (!value) return true;
  return data.name.includes(value);
};

onMounted(() => {
  deptTreeData.value = [
    {name: 'pc'},{name: 'app'}
  ]
  getList();
});
const getList = async (page) => {
  searchLoading.value = true;
  formInline.value.pageIndex = page ? page : formInline.value.pageIndex;

  tableData.value = list
  searchLoading.value = false
};
const load = (row,node,resolve)=>{
  setTimeout(() => {
    resolve([
        ...listLazy
    ])
  }, 1000)
}

const editHandle = (row)=>{
  router.push({
    path: '/menuManagementAddOrEdit',
    query: {
      type: 'edit',
      id: row.id,
    }
  })
}
const addChildHandle = (row)=>{
  router.push({
    path: '/menuManagementAddOrEdit',
    query: {
      type: 'addChild',
      id: row.id,
      parentId: row.parentId
    }
  })
}

const statueChange = (row,type)=>{
  let message = type ? `确定启用该菜单？`: `确定停用该菜单`
  ElMessageBox.confirm(message,'提示',{
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  }).then(()=>{
    const params = {
      id: row.id,
      status: type ? 1: 0
    }
    console.log(params, '123123')
  }).catch(()=>{})
}
const deleteMenuHandle = (row)=>{
  ElMessageBox.confirm('确定删除该菜单？','提示',{
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  }).then(()=>{

  }).catch(()=>{})
}


const onSubmit = () => {
  console.log("submit!");
  formInline.value = {
    workNumber: "",
    name: "",
    carType: "",
    departmentId: "",
    // date: "",
    pageIndex: 1,
    pageSize: 20,
  };
  getList();
};
const handleSizeChange = (val) => {
  getList();
};
const handleCurrentChange = (val) => {
  getList();
};
</script>


<style scoped lang="scss">
.content {
  display: flex;
  height: 100%;

  .container-box-left {
    width: 279px;
    padding: 10px 18px;
    // background-color: #ffffff;
    border-radius: 8px;

    .list-content {
      margin-top: 10px;
      height: calc(100% - 100px);
      overflow: auto;
    }
  }
  .container-box-right {
    margin-left: 10px;
    width: 0;
    flex: 1;
  }
}

.title {
  line-height: 40px;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .title-info {
    font-size: 16px;
    display: flex;
    align-items: center;
    img {
      margin-right: 10px;
    }
  }
  .title-tips {
    font-size: 12px;
    color: #fe5e5e;
  }
}
.custom-tree-node {
  width: 0;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
